const Generator = require("yeoman-generator");
const fs = require("fs");
const path =require("path");

module.exports = class extends Generator {

    prompting () {
        return this.prompt([
            {
                type : "input",
                name : "name",
                message : "Yor project name",
                default : this.appname
            }
        ]).then (answers => {
            this.answers  = answers;
        })
    }

    writing () {
        
        // 获取templates文件夹下所有文件
        let files = getAllFiles(this.sourceRoot());
        console.log(files);
        files.forEach( file => {
            let filePath = path.relative(this.sourceRoot(), file);
            console.log(filePath);
            this.fs.copyTpl(this.templatePath(filePath), this.destinationPath(filePath), this.answers);
        })
    }
}


function getAllFiles (root) {
    let result = [],
        files = fs.readdirSync(root);
    files.forEach( file => {
        var pathname = `${root}/${file}`,
            state = fs.lstatSync(pathname);
        if (state.isDirectory()) {
            result = result.concat(getAllFiles(pathname));
        } else {
            result.push(pathname);
        }
    })
    return result;
}